#!/bin/bash
[ -n "$1" ] && PHP_VER="$1"
[ -z "$1" ] && PHP_VER="8.1.9"
DOWNLOAD_DIR="/home/tools" #下载的路径
INSTALL_DIR="/usr/local"   #安装的路径
PHP_SRC="https://www.php.net/distributions/php-${PHP_VER}.tar.gz"
PHP_CFG="--prefix=${INSTALL_DIR}/php${PHP_VER} --with-openssl --disable-debug --disable-rpath  --disable-mbregex --enable-fpm --enable-shared --enable-soap --enable-phar --enable-calendar --enable-dom --enable-exif --enable-fileinfo --enable-filter --enable-ftp --enable-gd --enable-bcmath  --enable-mbstring --enable-pdo --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mysqlnd --enable-mysqlnd-compression-support --enable-opcache --enable-pcntl --with-curl --with-cdb --with-openssl --with-mhash --with-zlib --with-iconv --with-gettext --with-freetype --with-jpeg --with-gmp --with-pdo-mysql=shared,mysqlnd --with-mysqli=shared,mysqlnd --with-pdo-sqlite --with-readline --with-xsl --with-pear --with-mysql-sock=/var/run/mysql/mysql.sock --with-config-file-path=${INSTALL_DIR}/php${PHP_VER}/etc --without-gdbm "
LOG_FILE="${DOWNLOAD_DIR}/installphp.log"
PHP_EXT_DIR=${DOWNLOAD_DIR}"/php-extensions"
# 是插件的
PHP_EXT_LIST=("redis-5.3.7" "mongodb-1.14.0" "swoole-5.0.0")
PHP_EXT_DownloadPath="http://pecl.php.net/get/"
MAKE_INSTALL="make && make install"
#Downloading install package
if [ ! -d "${DOWNLOAD_DIR}" ]; then
    mkdir -p ${DOWNLOAD_DIR}
fi
echo $(date +"%Y-%m-%d %H:%M:%S") ": 准备安装环境;" >${LOG_FILE}
cd ${DOWNLOAD_DIR}

# 检测wget工具是否安装
rpm -q wget &>/dev/null
if [ $? -ne 0 ]; then
    yum install wget -y
fi
rpm -q gcc &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y gcc
fi
rpm -q gcc-c++ &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y gcc-c++
fi
rpm -q libxml2-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make libxml2-devel
fi
rpm -q openssl-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make openssl-devel
fi
rpm -q sqlite-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make sqlite-devel
fi
rpm -q libcurl-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make libcurl-devel
fi
rpm -q libpng-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make libpng-devel
fi
rpm -q libjpeg-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make libjpeg-devel
fi
rpm -q freetype &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make freetype
fi
rpm -q freetype-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make freetype-devel
fi
rpm -q gmp-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make gmp-devel
fi
rpm -q readline-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make readline-devel
fi
rpm -q libxslt-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make libxslt-devel
fi
rpm -q m4 &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make m4
fi
rpm -q autoconf &>/dev/null
if [ $? -ne 0 ]; then
    yum install -y make autoconf
fi

#如果源文件不存在则下载
if [ ! -f "${DOWNLOAD_DIR}/php-${PHP_VER}.tar.gz" ]; then
    echo $(date +"%Y-%m-%d %H:%M:%S") ":开始下载PHP源文件;" >>${LOG_FILE}
    cd ${DOWNLOAD_DIR}
    wget -c $PHP_SRC
fi
#如果源文件存在，
if [ -f "${DOWNLOAD_DIR}/php-${PHP_VER}.tar.gz" ]; then
    #则检测是否已解压
    if [ ! -d "${DOWNLOAD_DIR}/php-${PHP_VER}" ]; then
        cd ${DOWNLOAD_DIR}
        tar -zxvf php-${PHP_VER}.tar.gz
    fi
fi
cd ${DOWNLOAD_DIR}/php-${PHP_VER}

${INSTALL_DIR}/php${PHP_VER}/bin/php -v &>/dev/null
if [ $? -ne 0 ]; then #判断php没有安装走下面
    if [ -f "main/php_config.h" ]; then
        make clean
    fi
    cd ${DOWNLOAD_DIR}/php-${PHP_VER}
    ./configure ${PHP_CFG}
    if [ $? -eq 0 ]; then
        echo $(date +"%Y-%m-%d %H:%M:%S") ":开始安装PHP;" >>${LOG_FILE}
        make && make install
        if [ $? -eq 0 ]; then
            echo $(date +"%Y-%m-%d %H:%M:%S")":PHP ${PHP_VER} 安装完成" >>${LOG_FILE}
            #软连接
            ln -s ${INSTALL_DIR}/php${PHP_VER}/bin/php /usr/bin/php${PHP_VER}
            #配置文件的操作
            cp php.ini-production ${INSTALL_DIR}/php${PHP_VER}/etc/php.ini #php的配置文件
            cp sapi/fpm/php-fpm.conf ${INSTALL_DIR}/php${PHP_VER}/etc/php-fpm.conf
            cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #php的自动启动
            cp ${INSTALL_DIR}/php${PHP_VER}/etc/php-fpm.d/www.conf.default ${INSTALL_DIR}/php${PHP_VER}/etc/php-fpm.d/www.conf
            chmod +x /etc/init.d/php-fpm
            chkconfig php-fpm on #添加到开机启动
        else
            echo $(date +"%Y-%m-%d %H:%M:%S")":PHP ${PHP_VER} 安装失败.原因>" 2>>${LOG_FILE}
        fi
    else
        echo $(date +"%Y-%m-%d %H:%M:%S") ":安装出错。出错信息为:" 2>>${LOG_FILE}
        exit 1
    fi
fi
echo $(date +"%Y-%m-%d %H:%M:%S")":开始安装扩展" >>${LOG_FILE}
#创建扩展下载目录 扩展安装出错且不要重新下载扩展就注释下面一行
[ ! -d ${PHP_EXT_DIR} ] && mkdir -p ${PHP_EXT_DIR}
if [ $? -eq 0 ]; then
    for PHP_EXT in ${PHP_EXT_LIST[*]}; do
        if [ ! -f "${PHP_EXT}.tgz" ]; then
            echo $(date +"%Y-%m-%d %H:%M:%S")":开始下载扩展${PHP_EXT}" >>${LOG_FILE}
            cd ${PHP_EXT_DIR}
            wget -c ${PHP_EXT_DownloadPath}${PHP_EXT}.tgz
        fi
        tar -zxvf ${PHP_EXT}.tgz
        cd ${PHP_EXT_DIR}"/"${PHP_EXT}
        if [ -f ./configure ]; then
            make clean
        fi
        ${INSTALL_DIR}/php${PHP_VER}/bin/phpize
        isExitSwoole=$(echo ${PHP_EXT} | grep "swoole") #判断是否是swoole的扩展
        if [[ "$isExitSwoole" != "" ]]; then
            ./configure --enable-openssl --with-php-config=${INSTALL_DIR}/php${PHP_VER}/bin/php-config && make && make install
        else
            ./configure --with-php-config=${INSTALL_DIR}/php${PHP_VER}/bin/php-config && make && make install
        fi
        if [ $? -eq 0 ]; then
            echo $(date +"%Y-%m-%d %H:%M:%S")":${PHP_EXT} 安装完成" >>${LOG_FILE}
        else
            echo $(date +"%Y-%m-%d %H:%M:%S")":${PHP_EXT} 安装失败,原因>" 2>>${LOG_FILE}
        fi
    done
else
    echo $(date +"%Y-%m-%d %H:%M:%S")": PHP扩展安装条件不具备。" >>${LOG_FILE}
    echo "PHP扩展安装条件不具备。 "
fi
